﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;
using System.Text;

namespace park.Equipment
{
    public partial class MeteredSetDefaultParkingSpaces : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                PageInit();
            }
        }

        private void PageInit()
        {
            string meteredId = Request.Params["meteredId"];
            string json = string.Empty;
            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();
            json = sl.GetDefaultParkingSpaces(meteredId);
            #region 测试用的 json 数据
            //json = "{DefaultParkingSpaces: ["
            //    +"{"
            //    + "Collector: \"收集器01\",Checked:\"true\","
            //    +"        Repeaters: ["
            //    +"        { Name: \"中继器01\",Checked: false },"
            //    +"        { Name: \"中继器02\",Checked: true },"
            //    +"        { Name: \"中继器03\",Checked: false }"
            //    +"        ]"
            //    +"    },"
            //    +"    {"
            //    + "        Collector: \"收集器02\",Checked:\"true\","
            //    +"        Repeaters: ["
            //    +"        { Name: \"中继器04\",Checked:true },"
            //    +"        { Name: \"中继器05\",Checked:false },"
            //    +"        { Name: \"中继器06\",Checked:true }"
            //    +"        ]"
            //    +"    }]}";
            #endregion

            StringBuilder sb = new StringBuilder();

            sb.Append("<dl>");

            JObject obj = JObject.Parse(json);
            for (int i = 0; i < obj["DefaultParkingSpaces"].Count(); i++)
            {
                TreeNode node = new TreeNode(obj["DefaultParkingSpaces"][i]["Collector"].ToString());
                node.Checked = obj["DefaultParkingSpaces"][i]["Checked"].ToString().ToLower() == "true" ? true : false;
                for (int j = 0; j < obj["DefaultParkingSpaces"][i]["Repeaters"].Count(); j++)
                {
                    TreeNode childNode = new TreeNode(obj["DefaultParkingSpaces"][i]["Repeaters"][j]["Name"].ToString());
                    childNode.Checked = obj["DefaultParkingSpaces"][i]["Repeaters"][j]["Checked"].ToString().ToLower() == "true" ? true : false;
                    node.ChildNodes.Add(childNode);
                }
                this.tv.Nodes.Add(node);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            StringBuilder json = new StringBuilder();
            json.Append("{DefaultParkingSpaces: [");

            for (int i = 0; i < tv.Nodes.Count; i++)
            {
                json.Append("{Collector: \""+tv.Nodes[0].Text+"\",Checked:\""+tv.Nodes[0].Checked.ToString()+"\",Repeaters: [{");
                for (int j = 0; j < tv.Nodes[i].ChildNodes.Count; j++)
                {
                    json.Append("{ Name: \"" + tv.Nodes[i].ChildNodes[j].Text + "\",Checked: " + tv.Nodes[i].ChildNodes[j].Checked .ToString()+ " }");
                    if (j < tv.Nodes[i].ChildNodes.Count - 1)
                        json.Append(",");
                }
                json.Append("]}"); 
                if (i < tv.Nodes.Count - 1)
                    json.Append(",");
            }

            json.Append("]}");

            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();
           
            bool flag = sl.SaveDefaultParkingSpaces(json.ToString());

            //bool flag = true;  //////////////////////////////////////  测试用的。。。 等接口。。

            string msg = flag ? "数据保存成功！" : "数据保存失败！";
               Page.ClientScript.RegisterStartupScript(GetType(),Guid.NewGuid().ToString(),"<script>alert(\""+msg+"\");</script>");

        }

        protected void tv_SelectedNodeChanged(object sender, EventArgs e)
        {
            this.tv.SelectedNode.Checked = this.tv.SelectedNode.Checked ? false : true;
        }
    }
}
